package br.com.arg.aplica.data;

import br.com.arg.aplica.model.CodigoModelo;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateful;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

@Stateful
//The @Model stereotype is a convenience mechanism to make this a
//request-scoped bean that has an
//EL name
//Read more about the @Model stereotype in this FAQ:
//http://sfwk.org/Documentation/WhatIsThePurposeOfTheModelAnnotation
@Named 
@SessionScoped 

public class CodigoListProducer {
	@Inject
	private EntityManager em;

	private Map<String, Object> modelo2Value;

	@Produces
	@Named
	public Map<String, Object> getModelo2Value() {
		List<String> results = em.createQuery("select cm.nome from CodigoModelo cm group by nome", String.class)
				.getResultList();

		modelo2Value = new LinkedHashMap<String, Object>();
		for (String s : results) {
			modelo2Value.put(s, s); // label, value
		}
		return modelo2Value;
	}

	private List<CodigoModelo> codigoModelos;

	@Produces
	@Named
	public List<CodigoModelo> getCodigoModelos() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<CodigoModelo> criteria = cb.createQuery(CodigoModelo.class);
		Root<CodigoModelo> codigoModelo = criteria.from(CodigoModelo.class);
		// Swap criteria statements if you would like to try out type-safe
		// criteria queries, a new
		// feature in JPA 2.0
		// criteria.select(codigoModelo).orderBy(cb.asc(codigoModelo.get(CodigoModelo_.name)));
		criteria.select(codigoModelo).orderBy(cb.asc(codigoModelo.get("sequencia")));
		codigoModelos = em.createQuery(criteria).getResultList();
		return codigoModelos;
	}

	private List<String> stringListCMs;

	@Produces
	@Named
	public List<String> StringListCMs() {
		String sql = "select cm.linha from CodigoModelo where nome='" + "ModeloEntityV01" + "'";
		stringListCMs = em.createQuery(sql, String.class).getResultList();
		return stringListCMs;
	}
}
